package bio.file.bytestream;

import java.io.*;

/**
 * Instruction：
 * Author：@author MaLi
 */
public class FileCopy {
    public static void transferFile(String src, String dest) {
        //使用字节流拷贝文件
        try (FileInputStream fis = new FileInputStream(src);
            FileOutputStream fos =  new FileOutputStream(dest)) {
            int len;
            byte[] buffer = new byte[8192];
            while ((len = fis.read(buffer)) != -1) {
                fos.write(buffer,0,len);
            }
            fos.flush();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void transferFileBuffered(String src, String dest) {
        //使用字节流拷贝文件
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
             BufferedOutputStream bos =  new BufferedOutputStream(new FileOutputStream(dest))) {
            int len;
            byte[] buffer = new byte[8192];
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer,0,len);
            }
            bos.flush();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void transferFileBufferSteam(String src, String dest) {
        //使用字节流拷贝文件
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
             BufferedOutputStream bos =  new BufferedOutputStream(new FileOutputStream(dest))) {
            int x;
            while ((x = bis.read()) != -1) {
                bos.write(x);
            }
            bos.flush();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
